library(tidymodels)
## -- Attaching packages ------------------------------ tidymodels 0.1.0 --
## v broom 0.5.6 v recipes 0.1.13
## v dials 0.0.8 v rsample 0.0.7
## v dplyr 1.0.0 v tibble 3.0.3
## v ggplot2 3.3.2 v tune 0.1.1
## v infer 0.5.2 v workflows 0.2.0
## v parsnip 0.1.3 v yardstick 0.0.6
## v purrr 0.3.4
## -- Conflicts --------------------------------- tidymodels_conflicts() --
## x purrr::discard() masks scales::discard()
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
## x recipes::step() masks stats::step()
library(tidyverse)
## -- Attaching packages ------------------------------- tidyverse 1.3.0 --
## v tidyr 1.1.0 v stringr 1.4.0
## v readr 1.3.1 v forcats 0.5.0
## -- Conflicts ---------------------------------- tidyverse_conflicts() --
## x readr::col_factor() masks scales::col_factor()
## x purrr::discard() masks scales::discard()
## x dplyr::filter() masks stats::filter()
## x stringr::fixed() masks recipes::fixed()
## x dplyr::lag() masks stats::lag()
## x readr::spec() masks yardstick::spec()
library(modeltime)
library(timetk)
library(lubridate)
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
df <- timetk::bike_sharing_daily
df <- df %>% select(date = dteday, cnt)
df %>%
ggplot(aes(x = date, y = cnt)) +
geom_line()
# Create data and make stationary
df <- df %>%
mutate(daily_change = cnt - lag(cnt, n = 1)) %>%
select(date, daily_change) %>%
drop_na() %>%
arrange(date)
train_data <- training(initial_time_split(df, prop = .8))
test_data <- testing(initial_time_split(df, prop = .8))
train_data %>% mutate(type = "train") %>%
bind_rows(test_data %>% mutate(type = "test")) %>%
ggplot(aes(x = date, y =daily_change, color = type)) +
geom_line()
arima_model <- arima_reg() %>%
set_engine("auto_arima") %>%
fit(daily_change~date, data = train_data)
## frequency = 7 observations per 1 week
prophet_model <- prophet_reg() %>%
set_engine("prophet") %>%
fit(daily_change~date, data = train_data)
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
tslm_model <- linear_reg() %>%
set_engine("lm") %>%
fit(daily_change~as.numeric(date) + factor(month(date, label = TRUE)), data = train_data)
arima_boosted_model <- arima_boost(learn_rate = .015, min_n = 2) %>%
set_engine("auto_arima_xgboost") %>%
fit(daily_change~date + as.numeric(date) + factor(month(date, label = TRUE)), data = train_data)
## frequency = 7 observations per 1 week
forecast_table <- modeltime_table(
arima_model,
prophet_model,
tslm_model,
arima_boosted_model
)
forecast_table %>%
modeltime_calibrate(test_data) %>%
modeltime_accuracy()
## # A tibble: 4 x 9
## .model_id .model_desc .type mae mape mase smape rmse rsq
## <int> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1 ARIMA(0,0,2) WITH NON-Z~ Test 883. 99.5 0.618 191. 1287. 2.49e-4
## 2 2 PROPHET Test 876. 118. 0.613 157. 1285. 4.42e-3
## 3 3 LM Test 881. 99.7 0.616 185. 1286. 4.84e-4
## 4 4 ARIMA(2,0,2) WITH NON-Z~ Test 880. 99.4 0.616 174. 1288. 2.40e-6
forecast_table %>%
modeltime_calibrate(test_data) %>%
modeltime_forecast(actual_data = test_data) %>%
plot_modeltime_forecast()
## Using '.calibration_data' to forecast.
## Warning: `group_by_()` is deprecated as of dplyr 0.7.0.
## Please use `group_by()` instead.
## See vignette('programming') for more help
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.
forecast_table %>%
modeltime_refit(df) %>%
modeltime_forecast(h = 7, actual_data = df) %>%
plot_modeltime_forecast()
## frequency = 7 observations per 1 week
## Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## frequency = 7 observations per 1 week
## Warning: Expecting the following names to be in the data frame: .conf_hi, .conf_lo.
## Proceeding with '.conf_interval_show = FALSE' to visualize the forecast without confidence intervals.
## Alternatively, try using `modeltime_calibrate()` before forecasting to add confidence intervals.